import request from '../utils/request';  // request 是 demo 项目脚手架中提供的一个做 http 请求的方法，是对于 fetch 的封装，返回 Promise
import {updateImages,listImages} from '../services/fetch';
import {deleteImages} from '../services/deleteimage';
import {getDetails} from '../services/getdetails'

import {searchImages} from '../services/fetch';

const delay = (millisecond) => {
  return new Promise((resolve) => {
    setTimeout(resolve, millisecond);
  });
};

export default {
  namespace: 'listimages',
  state: {
    data: [],
    counter: 0,
      },
  effects: {
    *listImage(_, sagaEffects) {
      const { call, put } = sagaEffects;
      const endPointURI = '/dev/random_joke';

	 // 加入 try catch 捕获抛错 listImages
    const puzzle = yield call(listImages,endPointURI);
    console.log(puzzle.info.data);
    yield put({ type: 'addNewCard', payload: puzzle.info.data });

  
    },
    *deleteImage({payload}, sagaEffects) {
      const { call, put } = sagaEffects;
      const endPointURI = '/dev/random_joke';

   // 
      console.log("the",payload);
      const puzzle = yield call(deleteImages,payload);
      //console.log(puzzle.info.data);
      const puzzle_res = yield call(listImages,payload);

      yield put({ type: 'addNewCard', payload: puzzle_res.info.data });

  
    },
    *searchImage({payload},sagaEffects){
      const { call, put } = sagaEffects;
      const puzzle = yield call(searchImages,payload);
      yield put({ type: 'searchNewCard', payload: puzzle.info.data });
      console.log("the puzzle is ",puzzle.info.data);

    },
    *getImage({payload}, sagaEffects) {
      const { call, put } = sagaEffects;
      const endPointURI = '/dev/random_joke'; 
      
      const puzzle = yield call(getDetails,payload.id);
      console.log("before update get details",puzzle);
      //console.log(puzzle.info.data);
      //const puzzle_res = yield call(listImages,payload);

      yield put({ type: 'updateNewCard', payload: puzzle_res.info.data });

  
    },
        *updateImage({payload}, sagaEffects) {
      const { call, put } = sagaEffects;
      const endPointURI = '/dev/random_joke'; 
      
      const puzzle = yield call(updateImages,payload);
      console.log("before update get details",puzzle);
      //console.log(puzzle.info.data);
      //const puzzle_res = yield call(listImages,payload);
      //return puzzle;
      yield put({ type: 'updateNewCard', payload: puzzle.info.data });
      return puzzle;

  
    },
  },
  reducers: {
    addNewCard(state, { payload: newCard }) {
      //console.log("the new card is ",newCard);
      const nextCounter = state.counter + 1;
      //state.data.pop();
      //const newCardWithId = { ...newCard, id: nextCounter };
      //console.log("the newCardWithId",newCardWithId);
      //const nextData = state.data.concat(newCard);//list
      //console.log("the nextdata is",nextData);
      return {
        data: newCard,
        counter: nextCounter,
      };
    },
    deleteCard(state, { payload: newCard }) {
      //console.log("the new card is ",newCard);
      const nextCounter = state.counter + 1;
      //state.data.pop();
      //const newCardWithId = { ...newCard, id: nextCounter };
      //console.log("the newCardWithId",newCardWithId);
      //const nextData = state.data.concat(newCard);//list
      //console.log("the nextdata is",nextData);
      return {
        data: state.data.pop(),
        counter: nextCounter,
      };
    },
      searchNewCard(state, { payload: newCard }) {
      //console.log("the new card is ",newCard);
      const nextCounter = state.counter + 1;
      //state.data.pop();
      //const newCardWithId = { ...newCard, id: nextCounter };
      //console.log("the newCardWithId",newCardWithId);
      //const nextData = state.data.concat(newCard);//list
      //console.log("the nextdata is",nextData);
      return {
        data: newCard,
        counter: nextCounter,
      };
    },
    updateNewCardd(state, { payload: newCard }) {
      //console.log("the new card is ",newCard);
      const nextCounter = state.counter + 1;
      //state.data.pop();
      //const newCardWithId = { ...newCard, id: nextCounter };
      //console.log("the newCardWithId",newCardWithId);
      //const nextData = state.data.concat(newCard);//list
      //console.log("the nextdata is",nextData);
      return {
        data: newCard,
        counter: nextCounter,
      };
    },

  },
};